<template>
  <div>
    <el-form label-width="100px">
      <el-form-item label="权限名称">
        <el-input v-model="formData.name" />
      </el-form-item>
      <el-form-item label="权限标识">
        <el-input v-model="formData.code" />
      </el-form-item>
      <el-form-item label="权限描述">
        <el-input v-model="formData.description" />
      </el-form-item>
      <el-form-item label="权限启用">
        <el-switch
          v-model="formData.enVisible"
          active-text="启用"
          active-value="1"
          inactive-text="不启用"
          inactive-value="0"
        />
      </el-form-item>
      <el-form-item style="text-align:center">
        <el-button type="primary" @click="hSubmit">确定</el-button>
        <el-button @click="cHandler">取消</el-button>
      </el-form-item></el-form>
  </div>
</template>

<script>
import { addPermission, updatePermission } from '@/api/permisson'

export default {
  name: 'VueAdminTemplateMasterPerdia',
  props: {
    showDialog: {
      type: Boolean,
      default: false
    },
    row: {
      type: Object,
      default: () => {}
    },
    isEdit: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      formData: {
        name: '', // 名称
        code: '', // 权限标识
        description: '', // 描述
        enVisible: '0', // 开启
        pid: '', // 添加到哪个节点下
        type: 1 // 类型
      }
    }
  },
  watch: {
    isEdit: {
      handler(newVal) {
        if (newVal) {
        //   console.log(this.row)
        // 展开运算符
        //   this.form = { ...this.row }
        // JSON.parse
          this.formData = JSON.parse(JSON.stringify(this.row))
        //   console.log(this.form)
        }
      },
      immediate: true
    }
  },

  mounted() {

  },

  methods: {
    //   取消
    cHandler() {
      this.$emit('update:showDialog', false)
    },
    // 确认提交
    async hSubmit() {
      let res = {}
      if (!this.isEdit) {
        res = await addPermission(this.formData)
      } else {
        res = await updatePermission(this.formData)
      }
      //   console.log(res)
      if (res.code !== 10000) return
      this.$emit('update:showDialog', false)
      //   this.$emit('success')
      //   console.log(this.$parent.$parent)
      this.$parent.$parent.uploadPermissionList()
    }
  }
}
</script>

<style lang="scss" scoped>

</style>
